package GUI;

import java.awt.Rectangle;
import java.awt.TextArea;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;


import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import Client_logic.DealSale;
import Client_logic.Template;
import Client_logic.ClientNotInitializesException;

/**
 * This class use for show details of ActivedCampaign
 * @author ido
 * @author michael
 * @author matan
 * @author sharon
 *
 */
public class DealSaleForm extends TemplateForm
{
	private JLabel activeIDjLabel = null;
	private JLabel startDatejLabel = null;
	private JLabel endDatejLabel = null;
	private JLabel messagejLabel = null;
	private JTextField activeIDjTextField = null;
	private JTextField startDatejTextField = null;
	private JTextField endDatejTextField = null;
	private JComboBox statusJComboBox = null;
	private JButton nextjButton = null;
	private JButton updateAjButton = null;
	private Date startDate = null;
	private Date endDate = null;
	private DateFormat dateFormat = null;
	private DealSale dealsale = null;
	
	/**
	 * This is a default constructor 
	 */
	public DealSaleForm() 
	{
		super();
		initialize();
	}
	
	/**
	 * This method initializes DealSaleForm
	 */
	private void initialize() 
	{
		dealsale = new DealSale(0,null,null,null,null);
		activeIDjLabel = new JLabel();
		activeIDjLabel.setBounds(new Rectangle(17, 200, 107, 23));
		activeIDjLabel.setText("Active number");
		
		startDatejLabel = new JLabel();
		startDatejLabel.setBounds(new Rectangle(17, 230, 107, 23));
		startDatejLabel.setText("Start date");
		
		endDatejLabel = new JLabel();
		endDatejLabel.setBounds(new Rectangle(17, 260, 107, 23));
		endDatejLabel.setText("End date");
		
		messagejLabel = new JLabel();
		messagejLabel.setBounds(new Rectangle(17, 290, 107, 23));
		messagejLabel.setText("Status");
		
		startDate = new Date();
		endDate = new Date();
		dateFormat = new SimpleDateFormat("yyyy/MM/dd");
		
		this.add(activeIDjLabel, null);
		this.add(startDatejLabel, null);
		this.add(endDatejLabel, null);
		this.add(messagejLabel, null);
		
		this.add(getActiveIDjTextField(), null);
		this.add(getStartDatejTextField(), null);
		this.add(getEndDatejTextField(), null);
		this.add(getStatusJTextField() , null);	
		super.getBackjButton().setBounds(140, 400, 100, 25);
		this.setSize(500, 500);	
		super.closeall();
	}
		
	
	/**
	 * @return the dealsale
	 */
	public DealSale getDealSale() {
		return dealsale;
	}

	/**
	 * @param dealsale the dealsale to set
	 */
	public void setDealSale(DealSale dealsale) {
		this.dealsale = dealsale;
	}

	/**
	 * This method initializes activeIDjTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getActiveIDjTextField() 
	{
		if (activeIDjTextField == null) 
		{
			activeIDjTextField = new JTextField();
			activeIDjTextField.setBounds(new Rectangle(140, 200, 107, 23));			
		}
		return activeIDjTextField;
	}
	
	/**
	 * This method initializes startDatejTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getStartDatejTextField() 
	{
		if (startDatejTextField == null) 
		{
			startDatejTextField = new JTextField();
			startDatejTextField.setBounds(new Rectangle(140, 230, 107, 23));
		}
		return startDatejTextField;
	}
	
	/**
	 * This method initializes endDatejTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getEndDatejTextField() 
	{
		if (endDatejTextField == null) 
		{
			endDatejTextField = new JTextField();
			endDatejTextField.setBounds(new Rectangle(140, 260, 107, 23));
			endDatejTextField.setText("yyyy/MM/dd");
		}
		return endDatejTextField;
	}
	
	/**
	 * This method initializes statusJTextField	
	 * 	
	 * @return java.awt.TextArea
	 */
	private JComboBox getStatusJTextField() 
	{
		if (statusJComboBox == null) 
		{
			statusJComboBox = new JComboBox();
			statusJComboBox.setBounds(new Rectangle(140, 290, 107, 23));
			statusJComboBox.addItem("Run");
			statusJComboBox.addItem("Stop");
			statusJComboBox.addItem("End");
		}
		return statusJComboBox;
	}
	
	/**
	 * This method initializes nextjButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	public JButton getNextjButton() 
	{
		if (nextjButton == null)
		{
			nextjButton = new JButton();
			nextjButton.setBounds(new Rectangle(260, 400, 100, 25));
			nextjButton.setText("Next>>");			
		}
		return nextjButton;
	}
	
	/**
	 * 
	 * @return boolean
	 */
	public boolean ifNext()
	{
		Date sdate, edate;
		long id;
		String status;
		try
		{
			sdate = convertStringToDate(startDatejTextField.getText());
			if (sdate == null)
			{
				JOptionPane.showMessageDialog(null, "Ilegal start date systax!","",JOptionPane.ERROR_MESSAGE);
				return false;
			}
			if (startDate.getYear() > sdate.getYear())
			{	
				System.out.println(sdate);
				JOptionPane.showMessageDialog(null, "Ilegal start date!","",JOptionPane.ERROR_MESSAGE);
				return false;
			}
			if (startDate.getMonth() > sdate.getMonth())
			{	
				System.out.println(sdate);
				JOptionPane.showMessageDialog(null, "Ilegal start date!","",JOptionPane.ERROR_MESSAGE);
				return false;
			}
			if (startDate.getDay() > sdate.getDay())
			{	
				System.out.println(sdate);
				JOptionPane.showMessageDialog(null, "Ilegal start date!","",JOptionPane.ERROR_MESSAGE);
				return false;
			}
			
			edate = convertStringToDate(endDatejTextField.getText());
			if (edate == null)
			{
				JOptionPane.showMessageDialog(null, "Ilegal end date systax!","",JOptionPane.ERROR_MESSAGE);
				return false;
			}
			if (sdate.after(edate))
			{	
					JOptionPane.showMessageDialog(null, "Ilegal end date!","",JOptionPane.ERROR_MESSAGE);
					return false;
			}
			id = Long.parseLong(activeIDjTextField.getText());
			if (id > 0)
			{
				status = (String)statusJComboBox.getSelectedItem();
				dealsale.setDealSaleID(id);
				dealsale.setStartDate(sdate);
				dealsale.setEndDate(edate);
				dealsale.setStatus(status);
				return true;
			}
			else
				JOptionPane.showMessageDialog(null, "Ilegal ID!","",JOptionPane.WARNING_MESSAGE);
			return false;
		}catch(Exception exc)	
		{
			JOptionPane.showMessageDialog(null, "Ilegal input!","",JOptionPane.WARNING_MESSAGE);
			return false;
		}
	}
	
	/**
	 * This method convert string ("yyyy/MM/dd") to date
	 * return Date on success or null on error
	 * @param datestr the string to convert
	 * @return Date or null on error
	 */
	private Date convertStringToDate(String datestr)
	{
		String year,month, day;
		try
		{
			year = datestr.substring(0,datestr.indexOf("/"));
			datestr = datestr.substring(datestr.indexOf("/")+1, datestr.length());
			month = datestr.substring(0,datestr.indexOf("/"));
			datestr = datestr.substring(datestr.indexOf("/")+1, datestr.length());
			day = datestr;
			Date sdate = new Date(Integer.parseInt(year)-1900 ,Integer.parseInt(month)-1,Integer.parseInt(day));
			return sdate;
		}catch(Exception exc)
		{
			return null;
		}
	}
	
	/**
	 * This method load dealsale to the FORM
	 * @param dealsale the dealsale to load
	 */
	public void loadDealSale(DealSale dealsale)
	{
		this.dealsale = dealsale;
		super.loadTemplate(dealsale.getTemplate());
		String status = dealsale.getStatus();
		if (status != null)		
			statusJComboBox.setSelectedItem(status);
		else
			statusJComboBox.setSelectedItem("Run");
		if (dealsale.getStartDate() != null)
			startDatejTextField.setText(dateFormat.format(dealsale.getStartDate()));
		else
			startDatejTextField.setText("");
		if (dealsale.getEndDate() != null)
			endDatejTextField.setText(dateFormat.format(dealsale.getEndDate()));
		else
			endDatejTextField.setText("yyyy/MM/dd");
		if (dealsale.getDealSaleID() > 0)
			activeIDjTextField.setText(String.valueOf(dealsale.getDealSaleID()));
		else
			activeIDjTextField.setText("");
	}
	
	/**
	 * This method add next button to the Form
	 */
	public void addNextjButton()
	{
		this.add(getNextjButton(), null);
		startDatejTextField.setText(dateFormat.format(startDate));
	}
	
	/**
	 * This method initializes updatejButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	public JButton getAUpdatejButton() 
	{
		if (updateAjButton == null)
		{
			updateAjButton = new JButton();
			updateAjButton.setBounds(new Rectangle(260, 400, 100, 25));
			updateAjButton.setText("Update");
			updateAjButton.addActionListener(new java.awt.event.ActionListener() 
			{
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{
					dealsale.setStatus((String)statusJComboBox.getSelectedItem());
					try 
					{
						String msg = dealsale.updateDealSale();
						if (msg != null)
						{
							if (msg.equals("OK"))
								JOptionPane.showMessageDialog(null, "Successfuly update");
							else
								JOptionPane.showMessageDialog(null, "Not update!","",JOptionPane.WARNING_MESSAGE);
						}
					} catch (ClientNotInitializesException e1) 
					{
						JOptionPane.showMessageDialog(null, "ERROR!","",JOptionPane.ERROR_MESSAGE);
					}
				}
			});	
		}
		return updateAjButton;
	}
	
	/**
	 * This method add UpdatejButton to form
	 */
	public void addUpdatejButton()
	{
		activeIDjTextField.setEditable(false);
		startDatejTextField.setEditable(false);
		endDatejTextField.setEditable(false);
		this.add(getAUpdatejButton() , null);		
	}
	
	
	
	
	
}
